<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>文章上传</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<fieldset class="layui-elem-field" style="margin-top: 20px;">
    <legend>基本信息</legend>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                    <input type="hidden" name="image" id="image">
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="demo1" width="80px" height="80px">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <select name="label" lay-filter="aihao">
                    <option value="技术历程">技术历程</option>
                    <option value="精彩生活">精彩生活</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="text" name="sort" lay-verify="title" autocomplete="off" value="1" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">简介</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" name="paper" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">编辑器</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea layui-hide" name="content" lay-verify="content" id="LAY_demo_editor"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
                <button type="close" class="layui-btn layui-btn-primary">取消</button>
            </div>
        </div>
    </form>
</fieldset>


<script src="/static/layui/layui.js" charset="utf-8"></script>
<script src="/static/js/jquery-3.1.1.min.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate','upload'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,upload = layui.upload
            ,laydate = layui.laydate;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/admin/banner/api/upload'
            ,method:'post'
            ,field:'files'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code != 200){
                    return layer.msg('上传失败');
                }
                //上传成功
                $('#image').val(res.data.src)
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        layedit.set({
            uploadImage: {
                url: '/admin/article/api/upload' //接口url
                ,type: 'post' //默认post
            }
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        var index = parent.layer.getFrameIndex(window.name);

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 1){
                    return '必选项不能为空';
                }
            }
        });


        //监听提交
        form.on('submit(demo1)', function(data){
            var s = data.field;
            var content = layedit.getContent(editIndex);
            var formdata = new FormData();
            formdata.append("title",s.title);
            formdata.append("image",s.image);
            formdata.append("label",s.label);
            formdata.append("sort",s.sort);
            formdata.append("paper",s.paper);
            formdata.append("content",content);
            $.ajax({
                url: "/admin/article/api/add",
                type: "post",
                dataType: "json",
                data: formdata,
                contentType: false,
                processData: false,
                success: function (d) {
                    if(d.code==200){
                        layer.msg("文章发表成功!");
                        setTimeout(function(){
                            window.parent.location.reload();
                        },500);
                    }else{
                        layer.msg("文章发表失败!");
                    }
                }
            });
            return false;
        });


        $('#close').click(function(){
            parent.layer.close(index);
        });
    });
</script>

</body>
</html>